﻿using Flurl.Http;
using Pay.Core;
using Pay.DTO;
using Pay.IService;
using Pay.TO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Security.Claims;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace Pay.H5.Controllers {
    public class HomeController : GeneralController {
        public HomeController() { }
        private IMerchantService _merchantService;
        private ITopicService _topicService;
        private IProductService _productService;
        private ISettingService _settingService;
        private ICategoryService _categoryService;

        public HomeController(IMerchantService merchantService, ISettingService settingService,
                                              ICategoryService categoryService, ITopicService topicService,
                                              IProductService productService) {
            _merchantService = merchantService;
            _topicService = topicService;
            _productService = productService;
            _settingService = settingService;
            _categoryService = categoryService;
        }
        public JsonResult GetData(int page) {
            var oem = _merchantService.Get(Models.RequestMethodh5.MerchantNO);
            var goodsquery = new GoodsQuery {
                merchant_id = oem.id,
                page = page,
                size = 4
            };
            var list = _productService.Get(goodsquery);
            return Json(ResultToJson.Json(list), JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 首页
        /// </summary>
        /// <returns>The index.</returns>
        public ActionResult Index() {
#if DEBUG
            var host = "h5.epalpay.com";
#else
var host = Request.Url.Host;
#endif
            var model = _merchantService.GetMerchantByDomain(host);

            if (model != null) {
                var list = _categoryService.GetAll(model.id);
                var MerchantNo = model.merchant_no;
                var goodsquery = new GoodsQuery();
                var query = new TopicQuery();
                query.merchant_id = model.id;
                goodsquery.merchant_id = model.id;
                var topiclist = _topicService.GetTopics(query);
                if (topiclist != null && topiclist.rows.Count > 0) {
                    var bannerlist = topiclist.rows.Where(x => x.show_type == 1).ToList();
                    var arealist = topiclist.rows.Where(x => x.show_type == 2).ToList();
                    var choicelist = topiclist.rows.Where(X => X.show_type == 3).ToList();
                    var topics = topiclist.rows.Where(x => x.show_type == 4).ToList();
                    var seewordlist = topiclist.rows.Where(X => X.show_type == 5).ToList();
                    ViewData["choice"] = choicelist;
                    ViewData["banner"] = bannerlist;
                    ViewData["area"] = arealist;
                    ViewData["seeword"] = seewordlist;
                    ViewData["topics"] = topics;
                }

                goodsquery.audit_status = 1;
                goodsquery.sale_status = 0;
                goodsquery.sell_out = false;
                goodsquery.size = 4;
                goodsquery.sort = 1;
                goodsquery.tag = "hot";

                var hotproduct = _productService.Get(goodsquery);
                goodsquery.tag = "better";
                var likeproduct = _productService.Get(goodsquery);
                ViewData["like"] = likeproduct;
                ViewData["hot"] = hotproduct;

                ViewData["MerchantNo"] = MerchantNo;
                ViewData["categrylist"] = list;

                ViewData["url"] = site.img_url;
            }
            return View();
        }

        public ActionResult Login() {
            return View();
        }
        public ActionResult Register() {
            return View();
        }
        /// <summary>
        /// 注册
        /// </summary>
        /// <returns>The post.</returns>
        /// <param name="moblie">Moblie.</param>
        /// <param name="password">Password.</param>
        /// <param name="name">Name.</param>
        /// <param name="code">Code.</param>
        public JsonResult RegisterPost(string moblie, string password, string name, string code, string pmobile, string mobile_code = "86") {
            var grade_id = _merchantService.GetGradeId(Models.RequestMethodh5.USER_LEVEL_CUSTOM, Models.RequestMethodh5.MerchantNO);
            var result = _merchantService.Register(new register_model {
                mobile = moblie, mobile_code = mobile_code, code = code, grade_id = grade_id, name = name, oem_no = Models.RequestMethodh5.MerchantNO,
                parent_no = pmobile, password = password, re_password = password
            });
            FormsCookieSave(result.mobile, new userCookie { roles = new List<string> { "Member" }, user_id = result.id, }, true);
            return Json(api_result, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 发送短信
        /// </summary>
        /// <param name="mobile"></param>
        /// <returns></returns>
        public JsonResult GetCode(string mobile) {
            var result = _settingService.SendGlbRegsms(new send_model { mobile = mobile, mobile_code = "86", use_type = "register" });
            if (result != "0") {
                api_result.result_code = 500;
                api_result.result_msg = "操作失败";
            }
            return Json(api_result, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <returns>The post.</returns>
        /// <param name="userName">User name.</param>
        /// <param name="password">Password.</param>
        public JsonResult LoginPost(string userName, string password) {
            var model = _merchantService.Login(userName, password, Models.RequestMethodh5.MerchantNO);
            if (model != null) {
                api_result.result_code = 500;
                api_result.result_msg = "账号或密码错误";
                return Json(api_result, JsonRequestBehavior.AllowGet);
            }
            var formsCookie = new FormsCookie();
            formsCookie.FormsCookieSave(model.mobile, new userCookie { roles = new List<string> { "Member" }, user_id = model.id }, true);
            return Json(api_result, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 退出
        /// </summary>
        /// <returns></returns>
        [AllowAnonymous]
        public RedirectResult Logout(string return_url) {
            FormsAuthentication.SignOut();
            if (!string.IsNullOrEmpty(return_url))
                return Redirect(return_url);
            return Redirect("/home/index");
        }

        #region 授权登陆
        public JsonResult PostAuthorization(string phone, string UserName) {
            var model = _merchantService.LoginByMobile(phone, Models.RequestMethodh5.MerchantNO);
            if (model != null) {
                var formsCookie = new FormsCookie();
                formsCookie.FormsCookieSave(model.name, new userCookie { roles = new List<string> { "Member" }, user_id = model.id }, true);
                return Json(ResultToJson.Json<object>(new {
                    msg = "http://h5.epalpay.com/Home/Index",
                    result_code = 1000,
                    cook = model.id
                }), JsonRequestBehavior.AllowGet);
            } else {
                var grade_id = _merchantService.GetGradeId(Models.RequestMethodh5.USER_LEVEL_CUSTOM, Models.RequestMethodh5.MerchantNO);
                var result = _merchantService.Register(new register_model {
                    mobile = phone, mobile_code = "86", grade_id = grade_id, name = UserName, oem_no = Models.RequestMethodh5.MerchantNO,
                    password = "123456glb", re_password = "123456glb"
                });

                if (result.id > 0) {
                    var formsCookie = new FormsCookie();
                    formsCookie.FormsCookieSave(model.name, new userCookie { roles = new List<string> { "Member" }, user_id = model.id }, true);
                    return Json(ResultToJson.Json<object>(new {
                        msg = "http://h5.epalpay.com/Home/Index",
                        result_code = 1000,
                        cook = model.id
                    }), JsonRequestBehavior.AllowGet);
                } else {
                    return Json(ResultToJson.Json<object>(new {
                        msg = "系统异常",
                        result_code = -1
                    }), JsonRequestBehavior.AllowGet);
                }

            }
        }
        #endregion
    }
}